package com.org.manhu.chargehelper.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.org.manhu.chargehelper.ChargeApplication;
import com.org.manhu.chargehelper.R;
import com.org.manhu.chargehelper.constant.Constant;
import com.org.manhu.chargehelper.tools.DateUtil;
import com.org.manhu.chargehelper.tools.JsonUtil;
import com.org.manhu.chargehelper.tools.LogUtils;
import com.org.manhu.chargehelper.tools.NetUtils;
import com.org.manhu.chargehelper.tools.ToastUtils;
import com.org.manhu.chargehelper.webservice.AsyncProcessData;

/**
 *
 * @文件名：ExchangeAdapter.java
 * @包名：com.org.manhu.Adapter
 * @作者：李朋辉 @ QQ：892839211
 * @创建时间：2014年11月27日 下午6:01:20
 * @描述：
 */
public class OrderListAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private List<Map<String, String>> mList;
	private AsyncProcessData request;
	private Context context;
	private int removeIndex = -1;
//	private Map<Integer, Boolean> isButtonShow = new HashMap<Integer, Boolean>();

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (Constant.CANCEL_EXCHANGE_GOODS_ORDER.equals(msg.obj.toString())) {
				LogUtils.d("TAG", msg.getData().getString("jsonStr"));
				Map<String, String> map = JsonUtil.parseAll(msg.getData()
						.getString("jsonStr"));
				if (map != null) {
					String tips = map.get("Description");
					if (map.get("Code").equals("1")) {
						mList.get(removeIndex).put("StateID", "-2");
						mList.get(removeIndex).put("StateName", "");
//						isButtonShow.remove(removeIndex);
						removeIndex = -1;
						OrderListAdapter.this.notifyDataSetChanged();
					} else {
						removeIndex = -1;
					}
					ToastUtils.showShort(context, tips);
				}
			}
		};
	};

	public OrderListAdapter(Handler handler, Context context,
			List<Map<String, String>> list) {
		inflater = LayoutInflater.from(context);
		this.context = context;
		mList = list;
		request = new AsyncProcessData();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_order_list, null);
			holder.timeTxt = (TextView) convertView
					.findViewById(R.id.list_time);
			holder.typeTxt = (TextView) convertView
					.findViewById(R.id.list_type);
			holder.stateTxt = (TextView) convertView
					.findViewById(R.id.list_state);
			holder.cancelBtn = (Button) convertView
					.findViewById(R.id.list_btn_cancel);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final Map<String, String> map = mList.get(position);
		holder.timeTxt.setText(DateUtil.formatDate(map.get("CreateTime")));
		holder.typeTxt.setText(map.get("Remark"));
		holder.stateTxt.setText(map.get("StateName"));
		if ("0".equals(map.get("StateID"))) {
			holder.cancelBtn.setVisibility(View.VISIBLE);
//			 isButtonShow.put(position, true);
		} else {
			holder.cancelBtn.setVisibility(View.GONE);
		}

//		if (isButtonShow.containsKey(position)) {
//			holder.cancelBtn.setVisibility(View.VISIBLE);
//		}

		holder.cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String authkey = ChargeApplication.getInitInfo().get("AuthKey");
				if (!TextUtils.isEmpty(authkey)) {
					if (removeIndex != -1) {
						ToastUtils.showShort(context, "正在取消订单，稍后重试");
						return;
					}
					removeIndex = position;
					cancelOrder(map, authkey);
				} else {
					ToastUtils.showShort(context, "无法获取用户的信息，请重新打开客户端重试");
				}
			}

		});

		return convertView;
	}

	/**
	 * @描述：
	 * @param map
	 * @param authkey 
	 */
	private void cancelOrder(final Map<String, String> map,
			String authkey) {
		if(NetUtils.isConnected(context)){
			request.getRequestData(handler,
					Constant.CANCEL_EXCHANGE_GOODS_ORDER, "AuthKey",
					authkey, "OrderID", map.get("ID"));
		}else{
			ToastUtils.showShort(context, "");
		}
		
	}
	
	public void setData(List<Map<String, String>> data) {
		mList.addAll(data);
	}

	public void clearData() {
		mList.clear();
	}

	class ViewHolder {
		TextView timeTxt, typeTxt, stateTxt;
		Button cancelBtn;
	}

}
